package cn.tworice.blog.service.impl;

import cn.tworice.blog.dao.FileDao;
import cn.tworice.blog.entity.Dir;
import cn.tworice.blog.entity.File;
import cn.tworice.blog.service.FileService;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件系统服务
 * @author 二饭
 * @email 1786705138@qq.com
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileDao fileDao;

    @Override
    public int insertFile(File file) {
        // 判断当前目录下有没有这个文件
        ArrayList<File> files=fileDao.findFileByDir(file);
        if(files.size()>0){
            return -1;
        }
        int i=fileDao.insertFile(file);

        return i;
    }

    @Override
    public Dir queryDirByDirId(Integer dir) {
        return fileDao.queryDirByDirId(dir);
    }

    @Override
    public int createDir(Dir dir) {
        return fileDao.createDir(dir);
    }

    @Override
    public RequestResult queryListByDirId(Integer dir) {
        ArrayList<File> files=fileDao.findFileByDirId(dir);// 通过目录id获取目录下文件
        ArrayList<Dir> dirs=fileDao.findChildDirByDirId(dir);// 通过目录id获取目录下子目录

        RequestResult result =new RequestResult();
        result.setCode(new StatusCode(200,"请求成功"));

        result.appendData("dirs", dirs);
        result.appendData("files",files);

        return result;
    }

    @Override
    public File queryFileById(Integer id) {
        return fileDao.queryFileById(id);
    }

    @Override
    public void delFileById(Integer id) {
        fileDao.delFileById(id);
    }

    /**
     * 验证保险箱密码
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public RequestResult checkSafePwd(String pwd) {
        RequestResult result=new RequestResult();
        int i=fileDao.checkSafePwd(pwd);
        if(i==1){
            result.setCode(new StatusCode(200,"保险箱密码正确！"));
            result.appendData("safeFiles",getSafeFile());
        }else {
            result.setCode(new StatusCode(403,"保险箱密码错误！"));
        }
        return result;
    }



    /**
     * 将文件放入保险箱
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public RequestResult addSafe(Integer id) {
        RequestResult result = new RequestResult();
        int i=fileDao.addSafe(id);
        if(i==1){
            result.setCode(new StatusCode(200,"成功加入保险箱！"));
        }else {
            result.setCode(new StatusCode(501,"服务器繁忙，请稍后再试！"));
        }
        return result;
    }

    /**
     * 将文件移出保险箱
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public RequestResult removeSafe(Integer id) {
        RequestResult result = new RequestResult();
        int i=fileDao.removeSafe(id);
        if(i==1){
            result.setCode(new StatusCode(200,"成功移出保险箱！"));
        }else {
            result.setCode(new StatusCode(501,"服务器繁忙，请稍后再试！"));
        }
        return result;
    }

    /**
     * 获取保险箱内文件
     * @author 二饭
     * @email 1786705138@qq.com
     */
    @Override
    public ArrayList<File> getSafeFile(){
        ArrayList<File> list=fileDao.findSafeFile();
        return list;
    }
}
